package com.nbsaas.boot.store.rest.resource;

import com.nbsaas.boot.store.api.apis.StoreCatalogApi;
import com.nbsaas.boot.store.data.entity.StoreCatalog;
import com.nbsaas.boot.store.api.domain.request.StoreCatalogRequest;
import com.nbsaas.boot.store.api.domain.response.StoreCatalogResponse;
import com.nbsaas.boot.store.api.domain.simple.StoreCatalogSimple;
import com.nbsaas.boot.store.rest.convert.StoreCatalogSimpleConvert;
import com.nbsaas.boot.store.rest.convert.StoreCatalogEntityConvert;
import com.nbsaas.boot.store.rest.convert.StoreCatalogResponseConvert;
import com.nbsaas.boot.store.data.repository.StoreCatalogRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.store.api.domain.request.StoreCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   门店分类-业务接口实现
*/
@Transactional
@Service
public class StoreCatalogResource extends BaseResource<StoreCatalog,StoreCatalogResponse, StoreCatalogSimple, StoreCatalogRequest>  implements StoreCatalogApi {

    @Resource
    private StoreCatalogRepository storeCatalogRepository;

    @Override
    public JpaRepositoryImplementation<StoreCatalog, Serializable> getJpaRepository() {
        return storeCatalogRepository;
    }

    @Override
    public Function<StoreCatalog, StoreCatalogSimple> getConvertSimple() {
        return new StoreCatalogSimpleConvert();
    }

    @Override
    public Function<StoreCatalogRequest, StoreCatalog> getConvertForm() {
        return new StoreCatalogEntityConvert();
    }

    @Override
    public Function<StoreCatalog, StoreCatalogResponse> getConvertResponse() {
        return new StoreCatalogResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<StoreCatalogSimple> list(PageRequest request) {
        StoreCatalogSimpleConvert convert=new StoreCatalogSimpleConvert();
        if (request instanceof StoreCatalogSearch){
             StoreCatalogSearch searchRequest=(StoreCatalogSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<StoreCatalogSimple> root(StoreCatalogSearch search) {
        ListResponse<StoreCatalogSimple> result=new  ListResponse<>();
        List<StoreCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


